"""
@Time : 2023/12/27 11:18
@Auth : Mr.Zhao
#Function : 数据驱动测试执行入口
"""
import os
from common.mail import Mail
from common.all_path import path
from common.result_mail import Result
from excel.Excel_ddt import ddt
import argparse
from common.mysql import Mysql

if __name__ == "__main__":
    # 参数设置
    parser = argparse.ArgumentParser()
    parser.add_argument('--type', type = str, default="web", choices = ['web','app','inter'], help = '用例类型')
    parser.add_argument('--casename', type = str, default = '电商项目用例', help = '项目用例文件（后缀为.xlsx的文件，无需添加后缀）')
    args = parser.parse_args()

    os.system('rd /s/q lib\\result')
    os.system('rd /s/q lib\\report')

    casename = args.casename
    if args.type == 'web':
        ddt.run_web_cases(f'{casename}.xlsx')
    elif args.type == 'app':
        ddt.run_app_cases(f'{casename}.xlsx')
    else:
        # 数据库修改
        mysql = Mysql()
        mysql.init_mysql()
        ddt.run_inter_cases(f'{casename}.xlsx')

    os.system('allure generate lib\\result -o lib\\report --clean')

    mail = Mail()
    # 处理相对路径为绝对路径
    mail.mail_info['filepaths'] = [path + f'lib\\result-{casename}.xlsx']
    mail.mail_info['filenames'] = [f'result-{casename}.xlsx']

    with open(path + 'lib\\' + mail.mail_info['mailmodule'], mode='r', encoding='utf8') as f:
        text = f.read()

    res = Result()
    r = res.get_res(path + f'lib\\result-{casename}.xlsx')
    print(r)

    for key in r:
        text = text.replace(key, str(r.get(key)))

    text = text.replace("MyColor","darkred")#样式自定义

    r = res.get_groups(path + f'lib\\result-{casename}.xlsx')
    tr = '<tr><td width="100" height="28" align="center" ' \
         'bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td>' \
         '<td width="80" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td>' \
         '<td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td>' \
         '<td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">状态</td></tr>'

    trs = ''
    for group in r:
        one_tr = tr.replace('分组信息', str(group[0]))
        one_tr = one_tr.replace('用例总数', str(group[1]))
        one_tr = one_tr.replace('通过数', str(group[2]))
        one_tr = one_tr.replace('状态', str(group[3]))
        trs += one_tr

    text = text.replace('mailbody', trs)
    mail.send(text)